using System.Collections.Generic;
using Voodoo.Sauce.Internal.DebugScreen;

namespace Voodoo.Sauce.Debugger
{
	public class IssuesCounter : BadgeCounter
	{
		private int _issuesCount;

		public override void Start()
		{
			Refresh();
			update?.Invoke(_issuesCount);
			//LogsTracker.OnLogReceived += LogsTrackerOnIssueMessageReceived;
		}

		public override void Stop()
		{
			//LogsTracker.OnLogReceived -= LogsTrackerOnIssueMessageReceived;
			update?.Invoke(_issuesCount);
		}

		private void Refresh()
		{
			_issuesCount = 0;
			//IReadOnlyCollection<LogMessage> issues = LogsTracker.Issues;
			// if (issues == null)
			// {
			// 	_issuesCount = -1;
			// 	return;
			// }
			// foreach (LogMessage item in issues)
			// {
			// 	if (MustIssueBeCounted(item))
			// 	{
			// 		_issuesCount++;
			// 	}
			// }
		}

		private void LogsTrackerOnIssueMessageReceived(LogMessage issueMessage)
		{
			if (MustIssueBeCounted(issueMessage))
			{
				_issuesCount++;
				update?.Invoke(_issuesCount);
			}
		}

		private static bool MustIssueBeCounted(LogMessage issueMessage)
		{
			if (DebugLogTypes.IsEnabled(issueMessage.logType))
			{
				return LogConsoleDebugScreen.MessageMatchesSearchText(issueMessage);
			}
			return false;
		}
	}
}
